//
//  HPAccountInfoView.swift
//  iPhone
//
//  Created by Toj on 7/18/23.
//

import UIKit

class HPAccountInfoView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
        setupMakeInitialize()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupMakeLayoutSubviews()
    }
    
    private lazy var infoView = UIView()
    
    private let imageViewWH: CGFloat = 30
    private lazy var headImageView: UIImageView = {
        let view = UIImageView()
        view.cm_cornerRadius = imageViewWH * 0.5
        view.cm_border(color: "#E8CD8C".to_color)
        return view
    }()
    
    private lazy var nicknameLabel: UILabel = {
        let view = UILabel()
        view.textColor = "#333333".to_color
        view.font = 14.to_boldFont
        return view
    }()
    
    private lazy var vipIcon: UIButton = {
        let view = UIButton()
        view.setImage("com_vip_icon_nor".to_image, for: .normal)
        view.setImage("com_vip_icon_sel".to_image, for: .selected)
        return view
    }()
    
    private let siginButtonHeight: CGFloat = 30
    private(set) lazy var signinButton: UIButton = {
        let view = UIButton()
        view.titleLabel?.font = 14.to_boldFont
        view.setTitleColor("#0087EB".to_color, for: .normal)
        view.setTitle("signIn".localized(), for: .normal)
        view.isHidden = true
        view.adjustsImageWhenDisabled = false
        view.adjustsImageWhenHighlighted = false
        return view
    }()
}

// MARK: - 点击事件/ 通知事件回调
@objc private extension HPAccountInfoView {
    
}

// MARK: - Public
extension HPAccountInfoView {
    
    // 商品页信息
    func setupCommodityInfo(isVIP: Bool = false) {
        // nicknameLabel.text = STUserModel.shared.meInfoDto?.nickname
        nicknameLabel.text = "Norman"
        
        // 头像部分
        let placeholder = "me_info_head_default"
        // let imageUrl = STUserModel.shared.meInfoDto?.avatar ?? ""
        let imageUrl = "http://cdn.startimestv.com/head/h_d.png"
        let url = URL(string: imageUrl)
        headImageView.kf.setImage(with: url, placeholder: placeholder.to_image)
        
        let borderColor = isVIP ? "#E8CD8C" : "#999999"
        headImageView.cm_borderColor = borderColor.to_color
        
        vipIcon.isSelected = isVIP
        nicknameLabel.textColor = isVIP ? "#663300".to_color : "#333333".to_color
        
        // 根据是否登录 确定信息
        // let islogin = STUserModel.shared.isLogin()
        let islogin = arc4random_uniform(2) == 1
        print("-->toj: islogin: \(islogin)")
        signinButton.isHidden = islogin
        infoView.isHidden = !islogin
    }
}

// MARK: - Private
private extension HPAccountInfoView {
    
}

// MARK: - 初始化设置
private extension HPAccountInfoView {
    
    func setupMakeAddSubviews() {
        addSubview(headImageView)
        addSubview(infoView)
        addSubview(signinButton)
        
        infoView.addSubview(nicknameLabel)
        infoView.addSubview(vipIcon)
    }
    
    func setupMakeLayoutSubviews() {
        
        headImageView.snp.makeConstraints { make in
            make.centerY.equalTo(self)
            make.left.equalTo(15)
            make.width.equalTo(imageViewWH)
            make.height.equalTo(imageViewWH)
        }
        
        infoView.snp.makeConstraints { make in
            make.edges.equalTo(self)
        }
        
        nicknameLabel.snp.makeConstraints { make in
            make.centerY.equalTo(infoView)
            make.left.equalTo(headImageView.snp.right).offset(10)
            make.right.equalTo(infoView.snp.right).offset(-10)
        }
        
        vipIcon.snp.makeConstraints { make in
            make.bottom.equalTo(headImageView).offset(3)
            make.right.equalTo(headImageView)
            make.width.equalTo(40)
            make.height.equalTo(16)
        }
        
        signinButton.titleLabel?.sizeToFit()
        let size = signinButton.titleLabel?.frame.size ?? .zero
        signinButton.snp.remakeConstraints { (make ) in
            make.top.bottom.equalTo(headImageView)
            make.left.equalTo(headImageView.snp.right).offset(10)
            make.width.equalTo(size.width + 2)
        }
    }
    
    func setupMakeInitialize() {
        setupCommodityInfo()
    }
}
